package com.yvtian.inv.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yvtian.inv.pojo.InvPlanEntity;
import com.yvtian.inv.dao.InvPlanMapper;
import com.yvtian.inv.pojo.InvPlanShopEntity;
import com.yvtian.inv.service.InvPlanService;
import com.yvtian.inv.service.InvPlanShopService;
import com.yvtian.om.pojo.OmOrderEntity;
import com.yvtian.utils.StringHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;

/**
 * (inv_plan)表服务实现类
 *
 * @author long
 * @since 2024-06-03 15:23:39
 */
@Service("invPlanService")
public class InvPlanServiceImpl extends ServiceImpl<InvPlanMapper, InvPlanEntity> implements InvPlanService {

    @Autowired
    private InvPlanShopService invPlanShopService;

    @Override
    public IPage<InvPlanEntity> findByPage(Page<InvPlanEntity> page, InvPlanEntity invPlanEntity) {
        LambdaQueryWrapper<InvPlanEntity> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvPlanEntity::getIsDel, 0);
        if (StringHelper.isNotEmpty(invPlanEntity.getCode())) {
            queryWrapper.like(InvPlanEntity::getCode, invPlanEntity.getCode());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getName())) {
            queryWrapper.like(InvPlanEntity::getName, invPlanEntity.getName());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getOrderCode())) {
            queryWrapper.like(InvPlanEntity::getOrderCode, invPlanEntity.getOrderCode());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getOrderName())) {
            queryWrapper.like(InvPlanEntity::getOrderName, invPlanEntity.getOrderName());
        }
        if (invPlanEntity.getStatus() != null) {
            queryWrapper.eq(InvPlanEntity::getStatus, invPlanEntity.getStatus());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getBomCode())) {
            queryWrapper.like(InvPlanEntity::getBomCode, invPlanEntity.getBomCode());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getBomName())) {
            queryWrapper.like(InvPlanEntity::getBomName, invPlanEntity.getBomName());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getPlanStatus())) {
            queryWrapper.eq(InvPlanEntity::getPlanStatus, invPlanEntity.getPlanStatus());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getProjectCode())) {
            queryWrapper.like(InvPlanEntity::getProjectCode, invPlanEntity.getProjectCode());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getProjectName())) {
            queryWrapper.like(InvPlanEntity::getProjectName, invPlanEntity.getProjectName());
        }
        if (StringHelper.isNotEmpty(invPlanEntity.getCreateName())) {
            queryWrapper.like(InvPlanEntity::getCreateName, invPlanEntity.getCreateName());
        }
        return this.page(page, queryWrapper);
    }

    @Transactional
    @Override
    public boolean insertOrUpdateAll(InvPlanEntity invPlanEntity) {
        ArrayList<InvPlanShopEntity> invPlanShopList = invPlanEntity.getInvPlanShopList();
        if (invPlanShopList != null && !invPlanShopList.isEmpty()) {
            LambdaUpdateWrapper<InvPlanShopEntity> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(InvPlanShopEntity::getPlanCode,invPlanEntity.getCode());
            invPlanShopService.remove(updateWrapper);
            invPlanShopService.saveBatch(invPlanShopList);
        }
        this.saveOrUpdate(invPlanEntity);
        return true;
    }
}

